package it.tukano.swingdeco.text;

import it.tukano.swingdeco.containers.JScrollPaneExt;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;

/**
 * A JTextPane extension
 * @author pgi
 */
public class JTextPaneExt extends JTextPane {

    private static final long serialVersionUID = 0;
    
    private JScrollPaneExt scroller;

    /**
     * Instance initializer
     */
    public JTextPaneExt() {
    }
    
    public JScrollPaneExt getScroller() {
        return
                scroller == null ? 
                scroller = new JScrollPaneExt(this) :
                scroller;
    }

    /**
     * Append some text to this panel with the given attributes
     * @param string the text to append
     * @param att the syle attributes
     */
    public JTextPaneExt append(String string, SimpleAttributeSet att) {
        StyledDocument document = getStyledDocument();
        try {
            document.insertString(document.getLength(), string, att);
            setCaretPosition(document.getLength());
        } catch (BadLocationException ex) {
            Logger.getLogger(JTextPaneExt.class.getName()).log(Level.SEVERE, null, ex);
        }
        return this;
    }

    /**
     * Clear the content of this text pane
     * @return this
     */
    public JTextPaneExt clear() {
        setText("");
        return this;
    }

    /**
     * Set the editable state of this text panel
     * @param state true to enable edit for this panel
     * @return this
     */
    public JTextPaneExt editable(boolean state) {
        setEditable(state);
        return this;
    }
}
